package com.cpos.base.Netty.handler.netty.handler;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

/*
*
*    Netty Handler类
*
* */
public class EchoServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext context,Object msg){
        ByteBuf buf = (ByteBuf) msg;
        System.out.println("service received"+ buf.toString(CharsetUtil.UTF_8));
        context.write(buf);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext context){
        context.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx,Throwable couse){
       couse.printStackTrace();
       ctx.close();
    }
}
